﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using System.Xml;
using System.IO;

public partial class controls_admin_Logo : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
            loadData();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string logo = hdfLogo.Value;
            string banner1 = hdfBanner1.Value;
            string banner2 = hdfBanner2.Value;
            string banner3 = hdfBanner3.Value;
            try
            {
                if (fupLogo.HasFile)
                {
                    logo = fupLogo.FileName;
                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/home/") + logo))
                    {
                        logo = count + "_" + fupLogo.FileName;
                        count++;
                    }
                    fupLogo.SaveAs(Server.MapPath("~/upload/home/") + logo);
                }
                if (fulBanner.HasFile)
                {
                    banner1 = fulBanner.FileName;
                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/home/") + banner1))
                    {
                        banner1 = count + "_" + fulBanner.FileName;
                        count++;
                    }
                    fulBanner.SaveAs(Server.MapPath("~/upload/home/") + banner1);
                }
                if (fulBanner2.HasFile)
                {
                    banner2 = fulBanner2.FileName;
                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/home/") + banner2))
                    {
                        banner2 = count + "_" + fulBanner2.FileName;
                        count++;
                    }
                    fulBanner.SaveAs(Server.MapPath("~/upload/home/") + banner2);
                }
                if (fulBanner3.HasFile)
                {
                    banner3 = fulBanner3.FileName;
                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/home/") + banner3))
                    {
                        banner3 = count + "_" + fulBanner3.FileName;
                        count++;
                    }
                    fulBanner3.SaveAs(Server.MapPath("~/upload/home/") + banner3);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi ghi file! " + ex.Message);
                return;
            }
            string fname = Server.MapPath("~/upload/xml/banner.xml");
            XmlTextWriter xw = new XmlTextWriter(fname, null);
            xw.Formatting = Formatting.Indented;
            xw.Indentation = 6;
            xw.WriteStartDocument();
            xw.WriteStartElement("banner");
            //title
            xw.WriteStartElement("logo");
            xw.WriteString(logo);
            xw.WriteEndElement();
            xw.WriteStartElement("banner1");
            xw.WriteString(banner1);
            xw.WriteEndElement();
            xw.WriteStartElement("banner2");
            xw.WriteString(banner2);
            xw.WriteEndElement();
            xw.WriteStartElement("banner3");
            xw.WriteString(banner3);
            xw.WriteEndElement();
            //end
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công.");
            loadData();
        }
        catch (Exception)
        {
            throw;
        }
    }
    private void loadData()
    {
        try
        {
            string fname = Server.MapPath("~/upload/xml/banner.xml");
            if (File.Exists(fname))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fname);
                XmlNode root = doc.DocumentElement;
                XmlNodeList list = root.ChildNodes;
                string logo = list.Item(0).ChildNodes.Item(0).InnerText;
                string banner = list.Item(1).ChildNodes.Item(0).InnerText;
                string banner2 = list.Item(2).ChildNodes.Item(0).InnerText;
                string banner3 = list.Item(3).ChildNodes.Item(0).InnerText;
                hdfLogo.Value = logo;
                hdfBanner1.Value = banner;
                hdfBanner2.Value = banner2;
                hdfBanner3.Value = banner3;
                imgLogo.InnerHtml = "<img src='../upload/home/" + logo + "' alt='" + logo + "' title='" + logo + "' height = '100' />";
                imgBanner1.InnerHtml = "<img src='../upload/home/" + banner + "' alt='" + banner + "' title='" + banner + "' height = '100' />";
                imgBanner2.InnerHtml = "<img src='../upload/home/" + banner2 + "' alt='" + banner2 + "' title='" + banner2 + "' height = '100' />";
                imgBanner3.InnerHtml = "<img src='../upload/home/" + banner3 + "' alt='" + banner3 + "' title='" + banner3 + "' height = '100' />";
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi :<br />" + ex.Message);
        }
    }
}
